#coding=utf-8
#LightCam FireBird调用程序
#init by hfh 20150701

import socket,time,re,sys,traceback,threading,subprocess,os
import lc_libs


	
if __name__ == "__main__":


	lchome=lc_libs.lc_getlchome()
	scname=sys.argv[0].split('.')[0]
	logfilename=lchome+'/log/'+scname+'.log'
	confname=lchome+'/conf/'+scname+'.conf'
	
	today=time.strftime('%Y%m%d',time.localtime())
	os.system("mv %s %s.%s" % (logfilename,logfilename,today))
	
	logfile=file(logfilename,'a')
	logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	logfile.write(logdate+" Started.\n")
	logfile.flush()

	while True:
		time.sleep(1)
		realtime=int(time.time())
		if realtime%10==0:
			comd='''cd %s/scripts;LightCamPy lc_manager.py 172.28.161.150 1960 comd "D:\\monitor\\LightCam\\bin\\lc_firebird.exe"''' % lchome
			popen=subprocess.Popen(comd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
			time.sleep(1)
			for lines in popen.stdout.read().split('\n'):
				if "Agent:LightCam" in lines:
					comd="echo %s >> /var/log/lclog" % lines
					popen=subprocess.Popen(comd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
				elif "Temperature:" in lines:
					print lines
				elif "Humidity:" in lines:
					print lines
